﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DAL;
using System.Data.SqlClient;
using VOL;

namespace BLL
{
    public class NguoiDungBL
    {
        private DataAccess dataAccess;

        public NguoiDungBL()
        {
            dataAccess = new DataAccess();
        }

        /// <summary>
        /// 1. Hiển thị danh sách người
        /// </summary>
        /// <returns></returns>
        public DataTable GetData()
        {
            // cau query select
            string sql = "SELECT * FROM tbl_NguoiDung ORDER BY MaNguoiDung DESC ";

            DataTable dt = new DataTable();
            // do toan bo du lieu vao datatable
            dt = dataAccess.GetData(sql);
            return dt;
        }

        /// <summary>
        /// 2. Thêm người dùng
        /// </summary>
        /// <param name="obj">NguoiDung</param>
        /// <returns>true</returns>
        public bool InsertData(NguoiDung obj)
        {
            SqlParameter[] param = new SqlParameter[3];
            param[0] = new SqlParameter("@TenTruyCap", SqlDbType.NVarChar) { Value = obj.TenTruyCap };
            param[1] = new SqlParameter("@HoTen", SqlDbType.NVarChar) { Value = obj.HoTen };
            param[2] = new SqlParameter("@MatKhau", SqlDbType.NVarChar) { Value = obj.MatKhau };
            // cau query select
            string sql = "INSERT INTO tbl_NguoiDung  (TenTruyCap, HoTen, MatKhau) VALUES (@TenTruyCap, @HoTen, @MatKhau)";
            dataAccess.Execute(sql, param);

            return true;
        }

        /// <summary>
        /// 3. Xóa Nd
        /// </summary>
        /// <param name="MaNguoiDung">MaNguoiDung INT</param>
        /// <returns>true</returns>
        public bool DeleteData(int MaNguoiDung)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@MaNguoiDung", SqlDbType.NVarChar) { Value = MaNguoiDung };
            // cau query select
            string sql = "DELETE FROM tbl_NguoiDung WHERE MaNguoiDung = @MaNguoiDung";
            dataAccess.Execute(sql, param);

            return true;
        }

        /// <summary>
        /// 4. sửa nd
        /// </summary>
        /// <param name="obj">NguoiDung</param>
        /// <returns>true</returns>
        public bool UpdateData(NguoiDung obj)
        {
            SqlParameter[] param = new SqlParameter[4];
            param[0] = new SqlParameter("@TenTruyCap", SqlDbType.NVarChar) { Value = obj.TenTruyCap };
            param[1] = new SqlParameter("@HoTen", SqlDbType.NVarChar) { Value = obj.HoTen };
            param[2] = new SqlParameter("@MatKhau", SqlDbType.NVarChar) { Value = obj.MatKhau };
            param[3] = new SqlParameter("@MaNguoiDung", SqlDbType.Int) { Value = obj.MaNguoiDung };
            // cau query select
            string sql = "UPDATE tbl_NguoiDung SET TenTruyCap = @TenTruyCap, HoTen = @HoTen, MatKhau = @MatKhau " +
                           " WHERE MaNguoiDung = @MaNguoiDung";
            dataAccess.Execute(sql, param);

            return true;
        }

        /// <summary>
        /// 5. Tìm theo Mã nd
        /// </summary>
        /// <param name="MaNguoiDung"></param>
        /// <returns></returns>
        public DataTable GetDataById(int MaNguoiDung)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@MaNguoiDung", SqlDbType.NVarChar) { Value = MaNguoiDung };
            // cau query select
            string sql = "SELECT * FROM tbl_NguoiDung WHERE MaNguoiDung = @MaNguoiDung";
            DataTable dt = dataAccess.GetData(sql, param);

            return dt;
        }

        // 6. Login
        public NguoiDung Login(string tenTruyCap, string matKhau)
        {
            NguoiDung obj = new NguoiDung();

            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@TenTruyCap", SqlDbType.NVarChar) { Value = tenTruyCap };
            param[1] = new SqlParameter("@MatKhau", SqlDbType.NVarChar) { Value = matKhau };
            // cau query select
            string sql = "SELECT MaNguoiDung, HoTen FROM tbl_NguoiDung WHERE TenTruyCap = @TenTruyCap AND MatKhau = @MatKhau";
            DataTable dt = dataAccess.GetData(sql, param);
            if (dt.Rows.Count > 0)
            {
                obj.TenTruyCap = tenTruyCap;
                obj.MatKhau = matKhau;
                obj.MaNguoiDung = int.Parse(dt.Rows[0]["MaNguoiDung"].ToString());
                obj.HoTen = dt.Rows[0]["HoTen"].ToString();

                return obj;
            }

            return null;
        }
        // 7. Logout

        public bool ChangPass(NguoiDung obj)
        {
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@MatKhau", SqlDbType.NVarChar) { Value = obj.MatKhau };
            param[1] = new SqlParameter("@MaNguoiDung", SqlDbType.Int) { Value = obj.MaNguoiDung };
            // cau query select
            string sql = "UPDATE tbl_NguoiDung SET MatKhau = @MatKhau WHERE MaNguoiDung = @MaNguoiDung";
            dataAccess.Execute(sql, param);

            return true;
        }
    }
}
